package controller;

import com.jfinal.plugin.activerecord.Page;
import model.Type;
import model.User;
import service.ITypeService;
import service.IUserService;
import service.impl.TypeServiceImpl;
import service.impl.UserServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ykj on 16/1/11.
 */
public class TypeController extends BaseController {

    public void addDialog(){
        render("/admin/typeAddDialog.jsp");
    }

    public void updateDialog() {
        Integer id = getParaToInt("id");
        //没有id参数则返回401
        if(id==null) renderError(401);
        ITypeService typeService = new TypeServiceImpl();
        Type type = (Type) typeService.findById(id);
        setAttr("type",type);
        render("/admin/typeUpdateDialog.jsp");
    }

    public void save() {
        Type type = getModel(Type.class);
        ITypeService typeService = new TypeServiceImpl();
        Type result = (Type) typeService.save(type);
        int id = result != null ? result.getId() : -1;
        renderSaveResult(id, result);
    }

    public void update() {
        Type type = getModel(Type.class);
        ITypeService typeService = new TypeServiceImpl();
        Type result = (Type) typeService.update(type);
        int id = result != null ? result.getId() : -1;
        renderSaveResult(id, result);
    }

    public void delete() {
        Integer id = getParaToInt("id");
        //没有id参数则返回401
        if(id==null) renderError(401);
        ITypeService typeService = new TypeServiceImpl();
        boolean result = typeService.delete(id);
        renderDelResult(result);
    }

    public void getColumns() {
        List<String> fields = new ArrayList<String>() {{
            add("id");
            add("name");
        }};
        List<String> titles = new ArrayList<String>() {{
            add("标识");
            add("类型名称");
        }};
        renderColumns(fields, titles);
    }

    public void types() {
        ITypeService typeService = new TypeServiceImpl();
        renderData(typeService);
    }

    public void productTypes(){
        ITypeService typeService = new TypeServiceImpl();
        Page page = typeService.findByPage(1,99);
        renderJson(page.getList());
    }
}
